package com.example.book.advice;

import com.example.book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 */


@ControllerAdvice
@ResponseBody
@Slf4j
public class ErrorAdvice {
    @ExceptionHandler
    public Object handler(NullPointerException e) {
        log.error("发生异常: ", e);
        return Result.fail("NullPointerExceptiony异常,请联系管理员");
    }

    @ExceptionHandler
    public Object handler(ArithmeticException e) {
        log.error("发生异常: ", e);
        return Result.fail("ArithmeticExceptiony异常,请联系管理员");
    }
    @ExceptionHandler
    public Object handler(Exception e) {
        log.error("发生异常: ", e);
        return Result.fail("Exception异常,请联系管理员");
    }

}
